// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso;

import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.ConcurrentLinkedQueue;
import zombie.core.Core;
import zombie.debug.DebugLog;
import zombie.network.GameServer;
import zombie.vehicles.BaseVehicle;
import zombie.vehicles.VehiclesDB2;

public class ChunkSaveWorker {
    public static ChunkSaveWorker instance = new ChunkSaveWorker();
    private final ArrayList<IsoChunk> tempList = new ArrayList();
    public ConcurrentLinkedQueue<IsoChunk> toSaveQueue = new ConcurrentLinkedQueue();
    public boolean bSaving;

    public void Update(IsoChunk aboutToLoad) {
        if (!GameServer.bServer) {
            IsoChunk isoChunk0 = null;
            Object object = null;
            this.bSaving = !this.toSaveQueue.isEmpty();
            if (this.bSaving) {
                if (aboutToLoad != null) {
                    for (IsoChunk isoChunk1 : this.toSaveQueue) {
                        if (isoChunk1.wx == aboutToLoad.wx && isoChunk1.wy == aboutToLoad.wy) {
                            isoChunk0 = isoChunk1;
                            break;
                        }
                    }
                }

                if (isoChunk0 == null) {
                    isoChunk0 = (IsoChunk)this.toSaveQueue.poll();
                } else {
                    this.toSaveQueue.remove(isoChunk0);
                }

                if (isoChunk0 != null) {
                    try {
                        isoChunk0.Save(false);
                    } catch (Exception exception) {
                        exception.printStackTrace();
                    }
                }
            }
        }
    }

    public void SaveNow(ArrayList<IsoChunk> aboutToLoad) {
        this.tempList.clear();

        for (IsoChunk isoChunk0 = (IsoChunk)this.toSaveQueue.poll(); isoChunk0 != null; isoChunk0 = (IsoChunk)this.toSaveQueue.poll()) {
            boolean _boolean = false;

            for (int int0 = 0; int0 < aboutToLoad.size(); int0++) {
                IsoChunk isoChunk1 = (IsoChunk)aboutToLoad.get(int0);
                if (isoChunk0.wx == isoChunk1.wx && isoChunk0.wy == isoChunk1.wy) {
                    try {
                        isoChunk0.Save(false);
                    } catch (IOException iOException) {
                        iOException.printStackTrace();
                    }

                    _boolean = true;
                    break;
                }
            }

            if (!_boolean) {
                this.tempList.add(isoChunk0);
            }
        }

        for (int int1 = 0; int1 < this.tempList.size(); int1++) {
            this.toSaveQueue.add((IsoChunk)this.tempList.get(int1));
        }

        this.tempList.clear();
    }

    public void SaveNow() {
        DebugLog.log("EXITDEBUG: ChunkSaveWorker.SaveNow 1");

        for (IsoChunk isoChunk = (IsoChunk)this.toSaveQueue.poll(); isoChunk != null; isoChunk = (IsoChunk)this.toSaveQueue.poll()) {
            try {
                DebugLog.log("EXITDEBUG: ChunkSaveWorker.SaveNow 2 (ch=" + isoChunk.wx + ", " + isoChunk.wy + ")");
                isoChunk.Save(false);
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }

        this.bSaving = false;
        DebugLog.log("EXITDEBUG: ChunkSaveWorker.SaveNow 3");
    }

    public void Add(IsoChunk ch) {
        if (Core.getInstance().isNoSave()) {
            for (int _int = 0; _int < ch.vehicles.size(); _int++) {
                VehiclesDB2.instance.updateVehicle((BaseVehicle)ch.vehicles.get(_int));
            }
        }

        if (!this.toSaveQueue.contains(ch)) {
            this.toSaveQueue.add(ch);
        }
    }
}
